
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  sms;
import java.util.Date;

import logger.PoolLogger;
import dbmanager.DBManager;

public class Modem
{
	
	 private int delay;
	 PortHandler handle;
	 String modelName;
	 
	 PoolLogger pl;
	 
	public Modem(String commport, String baudrate)
	{
	     handle = new PortHandler(commport,baudrate);
	     handle.getComPort();
	     getModelname();
	     
	  // logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public Modem(PortHandler handle)
	{
	     this.handle = handle;
	     
	  // logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	public PortHandler getPortHandler()
	{
		return handle;
	}
	 public boolean SendSms(String MobileNo, String Message, int NoOfRetry)
	 {
	     String commandToExecute = "";
	     String modelName = getModelname();
	     for(int j = 0; j < NoOfRetry; j++)
	     {
	         String replyFromModem = "";
	         if(modelName.equals("HUAWEI"))
	         {
	             commandToExecute = (new StringBuilder()).append("AT$HSMSSD=").append(MobileNo).append(",").append(Message.length()).append(",").append("0,1").toString();
	             replyFromModem = handle.sendAndRecvCommand(commandToExecute, 30);
	         }
	         try
	         {
	             System.out.println((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.info((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.debug((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.warn((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.fatal((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.error((new StringBuilder()).append("SMS Delay==").append(delay).toString());
					pl.trace((new StringBuilder()).append("SMS Delay==").append(delay).toString());
	             Thread.sleep(delay);
	         }
	         catch(Exception e)
	         {
	             System.out.println((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
	             pl.info((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
					pl.debug((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
					pl.warn((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
					pl.fatal((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
					pl.error((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
					pl.trace((new StringBuilder()).append("ERROR IN THREAD==").append(e.getMessage()).toString());
	         }
	         if(replyFromModem.indexOf(">") != -1 || replyFromModem.indexOf("OK") != -1)
	         {
	             System.out.println((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.info((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.debug((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.warn((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.fatal((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.error((new StringBuilder()).append("This is Send Message").append(Message).toString());
					pl.trace((new StringBuilder()).append("This is Send Message").append(Message).toString());
	             if(modelName.equals("HUAWEI"))
	                 replyFromModem = handle.sendAndRecvCommand(Message, 50);
	         } else
	         {
	             replyFromModem = "ERROR";
	         }
	         if(replyFromModem.indexOf("OK") != -1)
	         {
	             System.out.println("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.info("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.debug("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.warn("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.fatal("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.error("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
					pl.trace("Message Successfully Sent\nMobileno :: "+MobileNo + "\n Message :: "+Message);
	             return true;
	         }
	         System.out.println("Modem is unable to Send Message");
				pl.info("Modem is unable to Send Message");
				pl.debug("Modem is unable to Send Message");
				pl.warn("Modem is unable to Send Message");
				pl.fatal("Modem is unable to Send Message");
				pl.error("Modem is unable to Send Message");
				pl.trace("Modem is unable to Send Message");
	         try
	         {
	             Thread.sleep(delay);
	         }
	         catch(Exception e)
	         {
	             e.printStackTrace();
	         }
	     }
	
	     System.out.println("SMS Sending failed ");
			pl.info("SMS Sending failed ");
			pl.debug("SMS Sending failed ");
			pl.warn("SMS Sending failed ");
			pl.fatal("SMS Sending failed ");
			pl.error("SMS Sending failed ");
			pl.trace("SMS Sending failed ");
	     return false;
	 }
	 
	 
	 public SMSDetails recieveSMS(int address)
	 {
		 SMSDetails smsd = new SMSDetails();
		 String message= null;
		 if(modelName.equals("HUAWEI"))
		    {
			 message = handle.sendAndRecvCommand("AT$HSMSRD="+address+" \r", 50);
			 //DELETE MESSAGE
		     handle.sendAndRecvCommand("AT+CMGD="+address+" \r", 50);
		    }
		    System.out.println("Message is :"+message);
			pl.info("Message is :"+message);
			pl.debug("Message is :"+message);
			pl.warn("Message is :"+message);
			pl.fatal("Message is :"+message);
			pl.error("Message is :"+message);
			pl.trace("Message is :"+message);
		    smsd.generate(message, modelName);
		    return smsd;
	 }
	 
	 public String getModelname()
	 {
		String  ATCommand = "AT+GMI";
		String modelName = handle.sendAndRecvCommand(ATCommand, 30);
		if(modelName.indexOf("ERROR") == -1 && modelName.indexOf("OK") >= 0)
		     if(modelName.indexOf("HUAWEI") >= 0)
	             modelName = "HUAWEI";
	         else
	             modelName = "Other";
		this.modelName = modelName; 
		return modelName;
			
	}
	 
		public static void main(String[] args) 
		{
			Modem send = new Modem("COM4","115200");
			//send.FindComPort();
			System.out.println(new Date());
			//for(int i=0;i<20;i++)			
				send.SendSms("9922930640", "Testing ...", 1);
			System.out.println(new Date());
		}

}